package com.example.combwx.widget;

import android.content.Context;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.Collections;
import java.util.List;

import cn.cyan.dragrecyclerview.OnItemChangeListener;

public abstract class SampleAdapter<T extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<T>implements OnItemChangeListener {

    protected Context context;
    protected volatile List<SampleEntity> data;

    public SampleAdapter(Context context, List<SampleEntity> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    @Override
    public boolean onItemDrag(int position) {
        return data.get(position).isDragEnable();
    }

    @Override
    public void onItemMoved(int form, int target) {
        if (form<target){
            for (int i = form;i < target; i++){
                Collections.swap(data,i,i+1);
            }
        }else {
            for (int i = form; i>target ; i--) {
                Collections.swap(data,i,i-1);
            }
        }
        notifyItemMoved(form,target);
    }

    @Override
    public boolean onItemDrop(int position) {
        return data.get(position).isDropEnable();
    }
}
